import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/css/flex.css'
Vue.use(ElementUI);

Vue.config.productionTip = false

router.beforeEach((to,from,next) => {
  //to 即将进入的路由
  //from即将离开的路由
  //设置tabs数据
  store.commit('getTabs');
  //设置激活选项卡
  store.commit('setActiveTabs',to.name);
  //如果store中菜单数据menu_data被刷新了，那么从新加载
  //从sessionStorage中获得数据
  let menuData=sessionStorage.getItem('menuList');
  //如果访问路位登录
  if(to.path==='/login'){
    //如果有数据
    if(menuData){
      //跳到首页
      next({path:'/home'})
    }else{
      next();
    }
  }else{
    //如果没有数据，路由不等于login
    if(!menuData && to.name!= 'login'){
      next({path: '/login'});
    }else{
      if(store.state.MenuStore.menu_data.length ==0) {
        store.commit('getMenuList',router);
        next({ path: to.path})
      }else{
        next(); //继续往下执行
      }
    }
  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
